﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ASmile.Win
{
    public class Global
    {
        public static event Action OnInitFinish;

        public static Task Init()
        {
            return Init(true);
        }

        public static Task Init(bool isLoadObjectConf = true)
        {
            try
            {
                //处理未捕获的异常
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //处理UI线程异常  
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                //处理非UI线程异常  
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                var task =
                Task.Factory.StartNew(() =>
                {
                    if (isLoadObjectConf)
                    {
                        Objects.ObjectLoader.LoadConf();
                    }
                    if (Config.Settings.IsRemoteLogin)
                    {
                        BLLBase bll = new BLLBase();
                        var ret = bll.KeepLive();
                        if (!ret)
                        {
                            LogHelper.LogObj.Error("远程服务器连接失败");
                            MessageBox.Show("远程服务器连接失败");
                            Application.Exit();
                            return;
                        }
                    }
                    OnInitFinish?.Invoke();
                });
                return task;
            }
            catch (Exception ex)
            {
                LogHelper.LogObj.Fatal("1-Main函数 出现应用程序未处理的异常", ex);
                MessageBox.Show("出现应用程序未处理的异常，详情查看日志！\r\n" + ex.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            string msg = "2-UI线程发生未处理异常!";
            string msg2 = string.Empty;
            if (e.Exception != null)
            {
                msg2 = "\r\n" + e.Exception.Message;
            }
            LogHelper.LogObj.Fatal(msg, e.Exception);
            MessageBox.Show($"{msg}{msg2}", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            string msg = "3-非UI线程发生未处理异常!";
            string msg2 = string.Empty;
            Exception error = e.ExceptionObject as Exception;
            if (error != null)
            {
                msg2 = "\r\n" + error.Message;
            }
            LogHelper.LogObj.Fatal(msg, error);
            MessageBox.Show($"{msg}{msg2}", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}